#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8:
# Author: xiyoulaoyuanjia
#         xiyoulaoyuanjia@gmail.com

import tornado.web



__all__ = ['BaseHandler', ]

class BaseHandler(tornado.web.RequestHandler):

    _application_export = ['db','message_queue', 'logger']
    def __getattr__(self, key):
        if key in self._application_export:
            return getattr(self.application, key)

        raise AttributeError('no such attr: %s' % key)


    def _render_module(self, module_file, module_name, **kwargs):
        module  = self.application.jinja_env.get_template(module_file).module
        return getattr(module, module_name)(**kwargs)

    def render_string(self, template_name, **kwargs):

        template = self.application.jinja_env.get_template(template_name)

        try:
            template = self.application.jinja_env.get_template(template_name)
        except jinja2.TemplateNotFound:
            raise

        namespace = dict(
                static_url=self.static_url,
#                xsrf_token=self.xsrf_token,

                handler=self,
#                request=self.request,
#                current_user=self.current_user,
#                locale=self.locale,
#                xsrf_form_html=self.xsrf_form_html,
#                reverse_url=self.reverse_url
            )
        namespace.update(kwargs)

        return template.render(namespace)




